/*
 * Copyright (c) 2009 Tamás Kovács <keccs *at* inbox.com>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package keccs.hitTheHay.ui;

import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.JSpinner.NumberEditor;

import keccs.hitTheHay.actions.StartCountdownAction;
import keccs.hitTheHay.actions.StopCountdownAction;
import keccs.hitTheHay.core.ApplicationModel;

import net.miginfocom.swing.MigLayout;

class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private TimePanel timePanel;

	JPopupMenu m = new JPopupMenu();

	public MainFrame() {
		super();
		ExitAction.setFrameToCloseOnExit(this);
		setupWindow();
		setupChildComponents();
		setupWindowBehavior();
		setFocusInTimeSpinner();
		setVisible(true);
	}

	private void setupWindow() {
		Image[] windowIcons = new Image[] { getBufferedImageIcon("appointment-new-16.png"), getBufferedImageIcon("appointment-new-22.png"), //$NON-NLS-1$ //$NON-NLS-2$
				getBufferedImageIcon("appointment-new-32.png"), getBufferedImageIcon("appointment-new-48.png") }; //$NON-NLS-1$ //$NON-NLS-2$
		setIconImages(Arrays.asList(windowIcons));
		setTitle(Messages.getString("MainFrame.WindowTitle")); //$NON-NLS-1$
		setResizable(false);
		setLocationByPlatform(true);
	}

	private static BufferedImage getBufferedImageIcon(String iconFileName) {
		try {
			URL iconURL = Main.class.getResource("/keccs/hitTheHay/resources/" + iconFileName); //$NON-NLS-1$
			if (iconURL == null) {
				System.err.println("Cannot read window icon: " + iconFileName); //$NON-NLS-1$
				return new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
			}
			return ImageIO.read(iconURL);
		} catch (IOException e) {
			System.err.println("Cannot read window icon"); //$NON-NLS-1$
			e.printStackTrace();
			return new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
		}
	}

	private void setupChildComponents() {
		setLayout(new MigLayout("flowy, fill")); //$NON-NLS-1$
		add(new ActionPanel(), "grow"); //$NON-NLS-1$
		timePanel = new TimePanel();
		add(timePanel, "grow"); //$NON-NLS-1$
		add(new ControlsPanel(), "growy 0, growx"); //$NON-NLS-1$
		pack();
	}

	private void setFocusInTimeSpinner() {
		NumberEditor timeSpinnerEditor = (NumberEditor) timePanel.timeSpinner.getEditor();
		JFormattedTextField timeSpinnerTextField = timeSpinnerEditor.getTextField();
		timeSpinnerTextField.requestFocusInWindow();
	}

	private void setupWindowBehavior() {
		ApplicationModel.getInstance().addPropertyChangeListener(new TitleUpdateListener(this));
		setupGlobalKeyBindings();
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				new ExitAction().actionPerformed(null);
			}
		});
	}

	private void setupGlobalKeyBindings() {
		getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"), "startCountdown"); //$NON-NLS-1$ //$NON-NLS-2$
		getRootPane().getActionMap().put("startCountdown", new StartCountdownAction()); //$NON-NLS-1$

		getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ESCAPE"), "stopCountdown"); //$NON-NLS-1$ //$NON-NLS-2$
		getRootPane().getActionMap().put("stopCountdown", new StopCountdownAction()); //$NON-NLS-1$
	}
	
	@Override
	public void dispose() {
		super.dispose();
		ApplicationModel.getInstance().stopCountdown();
	}
}